//
// Created by xili on 2024/8/2 20:18.
// Go big or go home.
//
#include <vector>
#include <unordered_map>
#include <climits>

using namespace std;

class Solution {
public:
    int minSubarray(vector<int> &nums, int p) {
        int n = nums.size();
        int ans = INT_MAX, cur = 0, find = 0;
        unordered_map<int, int> ump;
        ump[0] = -1;
        int mod = 0;
        for (auto &item: nums) {
            mod = (mod + item) % p;
        }
        if (!mod) {
            return 0;
        }
        for (int i = 0; i < n; i++) {
            cur = (cur + nums[i]) % p;
            find = (cur + p - mod) % p;
            if (ump.count(find)) {
                ans = min(ans, i - ump[find]);
            }
            ump[cur] = i;
        }
        return ans == n ? -1 : ans;
    }
};